package com.miage.m1.xml.project;

import java.io.IOException;
import java.util.List;

import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;

public class ClubReader implements XMLReader {
	
	private ContentHandler handler;
	private AttributesImpl atts = new AttributesImpl();

	/* Getter and setter */
	@Override
	public ContentHandler getContentHandler() {	return this.handler; }
	
	@Override
	public void setContentHandler(ContentHandler arg0) { this.handler = arg0; }
	
	/* Methods and Properties */
	@Override
	public void parse(InputSource input) throws IOException, SAXException {
		if(!(input instanceof ClubSource)){
			String m = "The only argument possible for parse() is a ClubSource";
			throw new SAXException(m);
		}
		if(this.handler == null){
			throw new SAXException("No ContentHandler");
		}
		
		ClubSource source = (ClubSource)input;
		List<Club> clubs = source.getClubs();
		
		handler.startDocument(); 
		handler.startElement("", "championship", "championship", atts);
		for(Club c : clubs){
			c.Serialize(handler);
		}
		handler.endElement("", "championship", "championship");
		handler.endDocument();

	}

	@Override
	public void parse(String arg0) throws IOException, SAXException {
		String m = "Le seul argument possible pour parse est une AnnuaireSource";
		throw new SAXException(m);
	}
	
	
	/* Other methods to implements */
	@Override
	public DTDHandler getDTDHandler() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EntityResolver getEntityResolver() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ErrorHandler getErrorHandler() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean getFeature(String arg0) throws SAXNotRecognizedException,
			SAXNotSupportedException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object getProperty(String arg0) throws SAXNotRecognizedException,
			SAXNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setDTDHandler(DTDHandler arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setEntityResolver(EntityResolver arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setErrorHandler(ErrorHandler arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setFeature(String arg0, boolean arg1)
			throws SAXNotRecognizedException, SAXNotSupportedException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setProperty(String arg0, Object arg1)
			throws SAXNotRecognizedException, SAXNotSupportedException {
		// TODO Auto-generated method stub

	}

}
